package com.lw.client;

import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;

/**
 * 客户端菜单界面
 */
public class QQView {

    private boolean loop = true;//是否显示菜单 true显示
    private String key = "";//用户键盘输入

    private UserClientService userClientService = new UserClientService();//登录注册
    private MessageClientService messageClientService = new MessageClientService();//私聊、群聊
    private FileClientService fileClientService = new FileClientService();//文件传输

    private void mainMenu() throws IOException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);

        while (loop){
            System.out.println("""
                    =========登录================
                    1 登录系统 
                    9 退出系统
                    """);
            key = scanner.nextLine();

            switch (key){
                case "1" -> {
                    System.out.print("用户名：");
                    String userId = scanner.nextLine();
                    System.out.print("密码：");
                    String pwd = scanner.nextLine();
                    //发送消息验证用户名和密码
                    boolean loginFlag = userClientService.checkUser(userId,pwd);
                    if(loginFlag){
                        System.out.println("登录成功："+userId);

                        while (loop){
                            System.out.println("""
                                    =========二级菜单============
                                    1. 显示用户在线列表
                                    2. 群发消息
                                    3. 私聊
                                    4. 发送文件
                                    9. 退出系统
                                    """);
                            System.out.print("输入选择：");
                            key = scanner.nextLine();
                            switch (key){
                                case "1"->{
                                    System.out.println("显示用户在线列表");
                                    userClientService.onlineFriendList();
                                }
                                case "2"->{
                                    System.out.println("群发消息，请输入消息（格式：  用户号[,用户号...]：消息内容）");
                                    String scanContent = scanner.nextLine();
                                    String receiveId = scanContent.split(":")[0];
                                    String content = scanContent.split(":")[1];
                                    //将消息内容发送给服务端
                                    this.messageClientService.sendMessageToAll(content,userId,receiveId);
                                }
                                case "3"->{
                                    System.out.println("请输入消息（格式：  在线用户号：消息内容）");
                                    String scanContent = scanner.nextLine();
                                    String receiveId = scanContent.split(":")[0];
                                    String content = scanContent.split(":")[1];
                                    //将消息内容发送给服务端
                                    this.messageClientService.sendMessageToOne(content,userId,receiveId);
                                }
                                case "4"->{
                                    System.out.println("发送文件");
                                    //这里固定死一个文件路径了，手输文件路径太长，接收方也固定死，不再手输
                                    File dir = new File("E:\\temp\\images");
                                    File[] files = dir.listFiles();

                                    Random random = new Random();
                                    int index = random.nextInt(files.length);
                                    String src = files[index].getAbsolutePath();
                                    String receiveId = "101";
                                    this.fileClientService.sendFileToOne(src,userId,receiveId);
                                }
                                case "9"->{
                                    loop = false;
                                }
                            }
                        }

                    }else {
                        System.out.println(">登录失败");
                    }
                }
                case "9"->{
                    loop = false;
                }
            }
        }

        this.userClientService.logout();
    }

    public static void main(String[] args) {
        try {
            new QQView().mainMenu();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        System.out.println("客户端退出");
    }

}
